package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.core.util.IDUtil;
import com.neusoft.core.util.Util;
import com.neusoft.neumooc.VO.OrderVO;
import com.neusoft.neumooc.service.OrderService;

@Controller
@RequestMapping(value = "/order")
public class OrderController extends BaseController {
	@Autowired
	OrderService orderService;

	@RequestMapping(value = "/addOrder")
	@ResponseBody
	public Map<String, Object> addOrder(HttpServletRequest request,
			HttpServletResponse response, OrderVO orderVO) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			String id = IDUtil.getID();
			String orderId = IDUtil.getID().substring(8, 24);
			orderVO.setId(id);
			orderVO.setOrderId(orderId);
			orderVO.setUserId("9AFEB8C633A348D2801DB4738F237052");

			int count = orderService.addOrder(orderVO);
			if (count > 0) {
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_MSG, "订单提交成功");
			} else {
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "订单提交失败");
			}
		} catch (Exception e) {
			log.error("提交订单有异常信息抛出" + e.getMessage(), e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "程序异常");
		}
		return retMap;
	}

	@RequestMapping(value = "/getOrder")
	@ResponseBody
	public Map<String, Object> getOrder(HttpServletRequest request,
			HttpServletResponse response, String userId, String pageNo,
			String pageSize) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		try {
			List<OrderVO> orders = orderService.getOrderList(userId, pageIndex,
					Util.parseInt(pageSize));
			int countNum = orderService.getOrderNum(userId);
			resMap.put("countNum", countNum);
			resMap.put("orders", orders);
		} catch (Exception e) {
			log.error("获取数据有异常信息抛出:" + e.getMessage(), e.getCause());
		}
		return resMap;
	}
}
